package com.second.app.thread.线程基础.day07.线程通信.唤醒通知;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author GYZ
 * @Date 2023/11/29
 * <pre>
 *  多生产 一个消费
 * </pre>
 */
public class ManyProducerToConsumeDemo {
    public static void main(String[] args) {
        //一个生产者 多个消费者

        MyStack02 myStack = new MyStack02();

        P2 p1 = new P2(myStack);
        P2 p2 = new P2(myStack);
        P2 p3 = new P2(myStack);
        P2 p4 = new P2(myStack);
        P2 p5 = new P2(myStack);
        P2 p6 = new P2(myStack);

        C2 c2 = new C2(myStack);

        p_thread02 p_thread01 = new p_thread02(p1);
        p_thread02 p_thread02 = new p_thread02(p2);
        p_thread02 p_thread03 = new p_thread02(p3);
        p_thread02 p_thread04 = new p_thread02(p4);
        p_thread02 p_thread05 = new p_thread02(p5);
        p_thread02 p_thread06 = new p_thread02(p6);

        p_thread01.start();
        p_thread02.start();
        p_thread03.start();
        p_thread04.start();
        p_thread05.start();
        p_thread06.start();

        C_thread02 c_thread02 = new C_thread02(c2);
        c_thread02.start();

    }
}
class MyStack02 {
    private final List<String> myList = new ArrayList<>();

    synchronized public void push() {
        try {
            //因为条件发生改变时，没有得到及时得响应，所以多个呈wait状态得线程被唤醒，继而执行
            //list.remove(0)代码出现异常，所以将if改成while语句即可
            while (myList.size() == 1) {
                this.wait();
            }
            myList.add("anyThing=" + Math.random());
            this.notifyAll();
            System.out.println("push=" + myList.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    synchronized public String pop() {
        String returnValue = "";
        try {
            while (myList.size() == 0) {
                System.out.println("pop 操作中的：" + Thread.currentThread().getName() + " 线程呈wait状态");
                this.wait();
            }
            returnValue = myList.get(0);
            myList.remove(0);
            this.notifyAll();
            System.out.println("pop=" + myList.size());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return returnValue;
    }
}


//案例1 一消费加一生产
class p_thread02 extends Thread {

    public P2 p2;

    public p_thread02(P2 p2) {
        this.p2 = p2;
    }

    @Override
    public void run() {
        while (true){
            p2.pushService();
        }
    }
}

class C_thread02 extends Thread {

    public C2 c2;

    public C_thread02(C2 c2) {
        this.c2 = c2;
    }

    @Override
    public void run() {
        while (true){
            c2.popService();
        }
    }
}

class P2 {
    private final MyStack02 myStack02;

    public P2(MyStack02 myStack02) {
        this.myStack02 = myStack02;
    }

    public void pushService() {
        myStack02.push();
    }
}

class C2 {
    private MyStack02 myStack02;

    public C2(MyStack02 myStack02) {
        this.myStack02 = myStack02;
    }

    public void popService() {
        System.out.println("pop="+myStack02.pop());
    }
}
